const Service = require('egg').Service;

class WeightService extends Service {
    constructor(ctx) {
        super(ctx);
    }

    async create(payload) {
        return await this.ctx.model.Weight.create(payload);
    }

    async insertMany(lists){
        const { ctx } = this;
        for(let item of lists){
            item.reporttime = ctx.helper.formatTime((new Date()).getTime());
        }
        return await this.ctx.model.Weight.bulkCreate(lists);
    }

    async index(payload) {
        const { ctx } = this;
        let { currentPage, pageSize, isPaging, search } = payload;
        let res = [];
        let count = 0;
        currentPage = currentPage ? Number(currentPage) : 1;
        pageSize = pageSize ? Number(pageSize) : 10;
        let offset = (currentPage - 1) * pageSize;
        let limit = pageSize;
        let where = {};

        res = await this.ctx.model.Weight.findAndCountAll({
            where,
            offset,
            limit,
            order:[['id', 'DESC']]
        });

        let lists = [];
        for(let item of res.rows){
            lists.push({
                createdAt: ctx.helper.formatTime((new Date(item.reporttime)).getTime()),
                grossTime: ctx.helper.formatTime(Number(item.grossTime)),
                tareTime: ctx.helper.formatTime(Number(item.tareTime)),
                no: item.no,
                carNo: item.carNo,
                productName: item.productName,
                icCode: item.icCode,
                grossWeight: item.grossWeight,
                tareWeight: item.tareWeight,
                netWeight: item.netWeight,
                sourceUnit: item.sourceUnit,
                transportUnit: item.transportUnit,
                handleUnit: item.handleUnit,
                status: item.status,
                result: item.result,
                flag: item.flag,
                deletetime: item.deletetime
            });
        }

        return { count: res.count, list: lists, pageSize: Number(pageSize), currentPage: Number(currentPage) };
    }

    async findLatest() {
        const { ctx } = this;
        const row = await ctx.model.Weight
        .findOne({
            order: [['tareTime', 'DESC']]
        });

        return row;
    }

    async findFailList() {
        const { ctx } = this;
        const Op = ctx.app.Sequelize.Op;
        const timestamp = (new Date()).getTime() - 60 * 60 * 1000;
        const res = await ctx.model.Weight
        .findAndCountAll({
            where: {
                [Op.or]: [
                    {
                        flag: 'fail',
                    },
                    {
                        flag: 'init',
                        reporttime: {
                            [Op.lt]: ctx.helper.formatTime(timestamp)
                        }
                    },
                    {
                        flag: null,
                        reporttime: {
                            [Op.lt]: ctx.helper.formatTime(timestamp)
                        }
                    }
                ]
            },
            offset: 0,
            limit: 20,
            order:[['id', 'DESC']]
        });

        return res.rows;
    }

    async updateFlag(ids, value) {
        const { ctx } = this;
        const Op = ctx.app.Sequelize.Op;
        let res = await this.ctx.model.Weight.update({
            flag: value
        }, {
            where: {
                no: {
                    [Op.in]: ids
                }
            }
        });
        return res;
    }

    async findYesterdayList() {
        const { ctx } = this;
        const Op = ctx.app.Sequelize.Op;
        const beginTimestamp = (new Date()).getTime() - 72 * 60 * 60 * 1000;
        const endTimestamp = (new Date()).getTime() - 12 * 60 * 60 * 1000;
        const res = await ctx.model.Weight
        .findAndCountAll({
            where: {
                tareTime: {
                    [Op.lt]: endTimestamp,
                    [Op.gt]: beginTimestamp
                },
                deletetime: null
            }
        });

        return res.rows;
    }

    async deleteByIds(ids) {
        const { ctx } = this;
        const Op = ctx.app.Sequelize.Op;
        let res = await this.ctx.model.Weight.update({
            status: 0,
            deletetime: ctx.helper.formatTime((new Date()).getTime())
        }, {
            where: {
                no: {
                    [Op.in]: ids
                }
            }
        });
        return res;
    }

    async findInIds(ids) {
        const { ctx } = this;
        const Op = ctx.app.Sequelize.Op;
        const res = await ctx.model.Weight
        .findAndCountAll({
            where: {
                no: {
                    [Op.in]: ids
                }
            }
        });

        return res.rows;
    }

    async findByRange(start, end) {
        const { ctx } = this;
        const Op = ctx.app.Sequelize.Op;

        const res = await ctx.model.Weight
        .findAndCountAll({
            where: {
                'grossTime': {
                    [Op.lt]: end,
                    [Op.gt]: start
                }
            }
        });

        return res.rows;
    }

    async findByWeightID(id) {
        const { ctx } = this;
        const row = await ctx.model.Weight
        .findOne({
            where: {
                no: id
            }
        });

        return row;
    }

    async updateStatus(ids, value) {
        const { ctx } = this;
        const Op = ctx.app.Sequelize.Op;
        let res = await this.ctx.model.Weight.update({
            status: value
        }, {
            where: {
                id: {
                    [Op.in]: ids
                }
            }
        });
        return res;
    }
}

module.exports = WeightService;
